package dao.impl;

import dao.BaseDao;
import dao.PersonDao;
import entity.Person;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PersonDaoImpl extends BaseDao implements PersonDao {


    @Override
    public List<Person> cx() throws SQLException, ClassNotFoundException {
        getConnection();
        List<Person> list=new ArrayList<>();
        PreparedStatement pr=conn.prepareStatement("select * from person");
        ResultSet se=pr.executeQuery();
        while (se.next()){
            Person pe=new Person();
            pe.setId(se.getInt("id"));
            pe.setName(se.getString("name"));
            pe.setSex(se.getString("sex"));
            pe.setAge(se.getInt("age"));
            pe.setRank(se.getString("rank"));
            pe.setDepartment(se.getString("department"));
            list.add(pe);
        }

        return list;
    }


    public int show(int a) throws SQLException, ClassNotFoundException {
        getConnection();
        String sql= "DELETE FROM person WHERE id=?";
        PreparedStatement statement=conn.prepareStatement(sql);
        statement.setInt(1,a);
        return statement.executeUpdate();
    }

    @Override
    public int insert(Person person) throws SQLException, ClassNotFoundException {
        getConnection();
        String sql= "INSERT INTO person(name,sex,age,rank,department) VALUES(?,?,?,?,?)";
        PreparedStatement statement=conn.prepareStatement(sql);
        statement.setString(1,person.getName());
        statement.setString(2,person.getSex());
        statement.setInt(3,person.getAge());
        statement.setString(4,person.getRank());
        statement.setString(5,person.getDepartment());
        return statement.executeUpdate();
    }
}